import '../util/sp_util.dart';

class UserInfo {
  static String uid = '';
  static String token = '';
  //可被通话
  static bool callable = true;

  static setCallable(bool b) {
    callable = b;
  }

  static login({Function? pre}) {
    if (pre != null) {
      pre();
    }
    var id = SpUtil.getString("id");
    var token = SpUtil.getString("token");
    UserInfo.uid = id!;
    UserInfo.token = token!;
  }

  static isLogin({Function? predicate}) {
    login();
    bool isLogin = UserInfo.uid != '';
    if (isLogin && predicate != null) {
      predicate();
    }
    return isLogin;
  }

  static void logout() {
    SpUtil.remove("id");
    SpUtil.remove("token");
    SpUtil.remove("loginInfo");
    UserInfo.uid = '';
    UserInfo.token = '';
  }
}

class ChatChannel {
  String channelID;
  int channelType;

  ChatChannel(this.channelID, this.channelType);
}

class CommonUtils {
  static String getAvatar(String channelID) {
    if (channelID == '') {
      return '';
    }
    return channelID.substring(0, 1);
  }

  static String formatDateTime(int timestamp) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    return "${dateTime.year}-${dateTime.month}-${dateTime.day} ${dateTime.hour}:${dateTime.minute}:${dateTime.second}";
  }
}
